package br.ufpe.cin.rdfilter.gui;

import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import br.ufpe.cin.rdfilter.feedback.Annotation;

/**
 * 
 * @author Rafael
 */
public class FrameAnnotation extends javax.swing.JFrame {
	private javax.swing.JButton btnAdd;
	private javax.swing.JButton btnEdit;
	private javax.swing.JButton btnRemove;
	private javax.swing.JButton btnBack;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	public javax.swing.JTable tableAnnotation;

	private static final long serialVersionUID = 1L;

	private FrameTool frameTool;
	private List<Annotation> annotatedTriple;
	private DefaultTableModel model;

	/** Creates new form FrameAnotacoes */
	public FrameAnnotation(List<Annotation> annotatedTriple,FrameTool frameTool) {
		initComponents();
		this.setTitle("Annotations");
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		tableAnnotation.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.setAnnotatedTriple(annotatedTriple);
		this.setFrameTool(frameTool);
		frameTool.enable(false);
		for (int i = 0; i < getAnnotatedTriple().size(); i++) {
			model.addRow(new Object[] {
					getAnnotatedTriple(i).getTriple().getSubject(),
					getAnnotatedTriple(i).getTriple().getPredicate(),
					getAnnotatedTriple(i).getTriple().getObject(),
					getAnnotatedTriple(i).getType(),
					getAnnotatedTriple(i).getConfidence() });
		}
		tableAnnotation.setFillsViewportHeight(true);
	}
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		jPanel1 = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		tableAnnotation = new javax.swing.JTable();
		btnEdit = new javax.swing.JButton();
		btnAdd = new javax.swing.JButton();
		btnRemove = new javax.swing.JButton();
		btnBack = new javax.swing.JButton();
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		jPanel1.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Annotations"));
		model = new javax.swing.table.DefaultTableModel(new Object[][] {},
				new String[] { "Subject", "Predicate", "Object", "Evaluation",
						"Confidence" }) {
			public boolean isCellEditable(int row, int col) {
				return false;
			}
		};
		tableAnnotation.setModel(model);
		jScrollPane1.setViewportView(tableAnnotation);
		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 550,
				Short.MAX_VALUE));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 191,
				Short.MAX_VALUE));
		btnEdit.setText("Edit");
		btnEdit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnEditActionPerformed(evt);
			}
		});
		btnAdd.setText("Add");
		btnAdd.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnAddActionPerformed(evt);
			}
		});
		btnRemove.setText("Remove");
		btnRemove.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnRemoverActionPerformed(evt);
			}
		});
		btnBack.setText("OK");
		btnBack.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnBackActionPerformed(evt);
			}
		});
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jPanel1,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addComponent(
														btnBack,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														89, Short.MAX_VALUE)
												.addComponent(
														btnRemove,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														89, Short.MAX_VALUE)
												.addGroup(
														javax.swing.GroupLayout.Alignment.LEADING,
														layout.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
																.addComponent(
																		btnEdit,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		Short.MAX_VALUE)
																.addComponent(
																		btnAdd,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		89,
																		Short.MAX_VALUE)))
								.addGap(20, 20, 20)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addComponent(
																		jPanel1,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(50, 50,
																		50)
																.addComponent(
																		btnEdit,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		31,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addComponent(
																		btnAdd,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		31,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																.addComponent(
																		btnRemove,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		31,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addGap(12, 12, 12).addComponent(btnBack)
								.addContainerGap()));
		pack();
	}// </editor-fold>

	private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
		frameTool.enable(true);
		this.dispose();
	}
	
	//If the line is selected show Frame Annotation
	private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {
		int selectedIndex = -1;
		selectedIndex = tableAnnotation.getSelectedRow();
		if (selectedIndex > -1 && selectedIndex < annotatedTriple.size()) {
			Annotation selectedAnnotation = annotatedTriple.get(selectedIndex);
			FrameAddEdit frameEdit = new FrameAddEdit(
					selectedAnnotation, frameTool, false, "Edit result",
					this);
			frameEdit.setVisible(true);

		} else
			JOptionPane.showMessageDialog(null,
					"Select a line of the table.");
	}

	private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
		FrameAddEdit frameAdd = new FrameAddEdit(null,
				frameTool, true, "Add result", this);
		frameAdd.setVisible(true);
	}

	private void btnRemoverActionPerformed(java.awt.event.ActionEvent evt) {
		int selectedIndex = tableAnnotation.getSelectedRow();
		if (selectedIndex != -1 && selectedIndex < annotatedTriple.size()) {
			model.removeRow(selectedIndex);
			annotatedTriple.remove(selectedIndex);
			restartTable();
		} else
			JOptionPane.showMessageDialog(null,
					"Select a line of the table");
	}

	public FrameTool getFrameTool() {
		return frameTool;
	}

	public void setFrameTool(FrameTool frameTool) {
		this.frameTool = frameTool;
	}

	public List<Annotation> getAnnotatedTriple() {
		return annotatedTriple;
	}

	public Annotation getAnnotatedTriple(int index) {
		return annotatedTriple.get(index);
	}

	public void setAnnotatedTriple(List<Annotation> annotatedTriple) {
		this.annotatedTriple = annotatedTriple;
	}

	public DefaultTableModel getModel() {
		return this.model;
	}

	public void setModel(DefaultTableModel model) {
		this.model = model;
	}

	public void restartTable() {
		this.getModel().getDataVector().removeAllElements();
		for (int i = 0; i < getAnnotatedTriple().size(); i++) {
			this.getModel()
					.addRow(new Object[] {
							getAnnotatedTriple().get(i).getTriple()
									.getSubject(),
							getAnnotatedTriple().get(i).getTriple()
									.getPredicate(),
							getAnnotatedTriple().get(i).getTriple().getObject(),
							getAnnotatedTriple().get(i).getType(),
							getAnnotatedTriple().get(i).getConfidence() });
		}
	}
}